﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using Feedback.Business;
using Feedback.Global;
using FeedbackWebClient.Common;

namespace FeedbackWebClient
{
    public partial class FeedbackView : BasePage
    {


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PopulateSortItem();
                PopulateFeedback();
            }
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            PopulateFeedback();
        }


        #region Event's Handler
        protected void btnPositiveFeedback_Click(object sender, EventArgs e)
        {
            List<int> selectedIdList = GetSelectedFeedbacks();
            if (selectedIdList.Count > 0)
            {
                if (new FeedbackLogic().UpdatePositiveFeedbacks(selectedIdList))
                {
                    PopulateFeedback();
                }
            }
        }

        protected void btnNegativeFeedback_Click(object sender, EventArgs e)
        {
            List<int> selectedIdList = GetSelectedFeedbacks();
            if (selectedIdList.Count > 0)
            {
                if (new FeedbackLogic().UpdateNegativeFeedbacks(selectedIdList))
                {
                    PopulateFeedback();
                }
            }
        }

        protected void btnDeleteFeedback_Click(object sender, EventArgs e)
        {
            List<int> selectedIdList = GetSelectedFeedbacks();
            if (selectedIdList.Count > 0)
            {
                if (new FeedbackLogic().DeleteFeedbacks(selectedIdList))
                {
                    PopulateFeedback();
                }
            }
        }

        protected void btnBlockFeedback_Click(object sender, EventArgs e)
        {
            List<int> selectedIdList = GetSelectedFeedbacks();
            if (selectedIdList.Count > 0)
            {
                if (new FeedbackLogic().BlockFeedbacks(selectedIdList))
                {
                    PopulateFeedback();
                }
            }
        }

        protected void ddlSortBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            var dataSource = new FeedbackLogic().GetAll(ddlSortBy.Text, StateHelper.BusinessId);
            rptFeedback.DataSource = dataSource;
            rptFeedback.DataBind();
        }
        #endregion

        #region Helper Methods

        private List<int> GetSelectedFeedbacks()
        {
            List<int> selectedIdList = new List<int>();
            for (int count = 0; count < rptFeedback.Items.Count; count++)
            {
                CheckBox chk = (CheckBox)rptFeedback.Items[count].FindControl("chkSelector");
                if (chk.Checked)
                {
                    HiddenField hiddenField = (HiddenField)rptFeedback.Items[count].FindControl("hfFeedback");
                    selectedIdList.Add(Convert.ToInt32(hiddenField.Value));
                }
            }
            return selectedIdList;
        }

        private void PopulateFeedback()
        {
            var dataSource = new FeedbackLogic().GetAll(StateHelper.BusinessId);
            rptFeedback.DataSource = dataSource;
            rptFeedback.DataBind();
        }

        private void PopulateSortItem()
        {
            ddlSortBy.Items.Add(new ListItem(WellKnownNames.PositiveFeedback));
            ddlSortBy.Items.Add(new ListItem(WellKnownNames.NegativeFeedback));
            ddlSortBy.Items.Add(new ListItem(WellKnownNames.FeedbackFromToday));
            ddlSortBy.Items.Add(new ListItem(WellKnownNames.FeedbackFromLastWeek));
            ddlSortBy.Items.Add(new ListItem(WellKnownNames.StarredFeedback));
        }
        #endregion

        #region Page Methods
        [WebMethod]
        public static bool MergeFeedback(string ownerId, string mergeId)
        {
            return new FeedbackLogic().MergeFeedback(ownerId, mergeId);
        }
        #endregion
    }
}